<?php

/**
 * @Author:      lyt
 * @DateTime:    2016-02-19 13:48:05
 * @Description: 用于入库的操作
 */
class ScsDatabaseStorageController extends BaseController
{
    /**
     * @Author:      lyt
     * @DateTime:    2016-02-19 13:51:44
     * @Description: 用于创建 批量临时 入库单的操作
     */
    public function createStorage(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'date'=>"required|date",
                'supplier'=>"required",
                'storage_type'=>"required|in:0,1,4",
                'pay_type'=>"in:0,1,2,3",
                'tax'=>"in:1,2",          //是否含税(1:含税;2:不含税;)
                'logistics_freight'=>"in:1,2",  //物流运费(1:含运费;2:不含运费;)
                'cust_id'=>"required",
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            if(MaintenanceInventoryList::isInventory($sc_user->shop_id)){
                $response = Lang::get("scs_error.data_inventory_isrun_61000");
                break;
            }

            //添加是否含税、物流运费、结算方式
            $tax = Input::get('tax');
            $logistics_freight = Input::get('logistics_freight');
            $pay_type = Input::get("pay_type");
            if ($pay_type == 555 || $tax == 555 || $logistics_freight == 555) {
                $response = Lang::get('scs_error.maintenance_supplier_pay_tax_freight_error_130001');
                break;
            }
            if ($pay_type != 3) {                               //不是盘盈的时候
                if (!in_array($tax, [1, 2])|| !in_array($logistics_freight, [1, 2])) {
                    $response = Lang::get('scs_error.maintenance_supplier_pay_tax_freight_error_130001');
                    break;
                }
            } else {
                $tax = 0;
                $logistics_freight = 0;
            }

            $msl=new MaintenanceStorageList;
            $id=Common::createId();
            $msl->id=$id;
            $msl->shop_id=$sc_user->shop_id;
            $msl->date=Input::get("date");
            $msl->supplier=Input::get("supplier");
            $msl->storage_type=Input::get("storage_type");
            $msl->pay_type=$pay_type;
            $msl->tax=$tax;
            $msl->logistics_freight=$logistics_freight;
            $msl->status=0;
            $msl->cust_id=Input::get("cust_id");
            $msl->type=1;
            $msl->desc=Input::get("desc");
            $msl->create_by=$sc_user->id;
            $msl->save();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("id"=>$id));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "createStorage", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      lyt
     * @DateTime:    2016-03-09 09:48:25
     * @Description: 用于创建 领用退库的 入库单操作 和 暂时临时领用一样 防止更改暂时 先分开接口
     */
    public function createStorageReturn(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'date'=>"required|date",
                'consuming_id'=>"required",
                'storage_type'=>"required|in:0,1,3,4",
                'methed'=>"required|in:1,2,3",
                'cust_id'=>"required",
                'shop_type'=>"required"
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            if(MaintenanceInventoryList::isInventory($sc_user->shop_id)){
                $response = Lang::get("scs_error.data_inventory_isrun_61000");
                break;
            }
            $msl=new MaintenanceStorageList;
            $id=Common::createId();
            $msl->id=$id;
            $msl->shop_id=$sc_user->shop_id;
            $msl->date=Input::get("date");
            $msl->supplier=0;
            $msl->storage_type=Input::get("storage_type");
            $msl->methed=Input::get("methed");
            if(Input::get('shop_type')==1){//1本店2店外
                $msl->user_type=Input::get("user_type");
                if(Input::get('user_type')==1){
                    $sc=SCUser::find(Input::get('consuming_id'));
                    $msl->team=$sc->team;
                }else{
                    $me=MaintenanceEngineers::find(Input::get('consuming_id'));
                    $msl->team=$me->team;
                }
            }
            $msl->shop_type=Input::get("shop_type");
            $msl->consuming_id=Input::get("consuming_id");
            $msl->pay_type=0;
            $msl->status=0;
            $msl->cust_id=Input::get("cust_id");
            $msl->type=2;
            $msl->desc=Input::get("desc");
            $msl->create_by=$sc_user->id;
            $msl->save();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("id"=>$id));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "createStorage", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      lyt
     * @DateTime:    2016-02-27 14:03:11
     * @Description: 用于删除 批量临时入库单 和 领用退库单
     * 新建的 批量临时入库可以删除 同时修改该入库单下的 所有耗材记录为 删除
     */
    public function  deleteStorage(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id'=>"required",
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $msl=MaintenanceStorageList::find(Input::get("id"));
            if(is_null($msl)||$msl->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.maintenance_storage_notexit_60000");
                break;
            }
            if($msl->status!=0){
                $response = Lang::get("scs_error.maintenance_storage_statuserror_60001");
                break;
            }
            if(MaintenanceInventoryList::isInventory($sc_user->shop_id)){
                $response = Lang::get("scs_error.data_inventory_isrun_61000");
                break;
            }
            $msl->status=3;
            $msl->save();
            MaintenanceStorageItem::where("list_id",$msl->id)->where("status",0)->update(array("status"=>2));
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("id"=>$msl->id));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "deleteStorage", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      lyt
     * @DateTime:  2017-08-15 14:02:20
     * @Description: 更换入库批次货位 暂时只修改该纪录的信息
     */
    public function  changeLocaStorage(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id'=>"required",
                'location'=>"required",
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $msi=MaintenanceStorageItem::find(Input::get("id"));
            if(is_null($msi)){
                $response = Lang::get("scs_error.maintenance_storage_notexit_60000");
                break;
            }
            $msl=MaintenanceStorageList::find($msi->list_id);
            if(is_null($msl)||$msl->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.maintenance_storage_notexit_60000");
                break;
            }
            if(MaintenanceInventoryList::isInventory($sc_user->shop_id)){
                $response = Lang::get("scs_error.data_inventory_isrun_61000");
                break;
            }
            $mscl=new MaintenanceStorageChangeLocation();
            $mscl->msi_id=$msi->id;
            $mscl->old_location=$msi->location;
            $mscl->new_location=Input::get("location");
            $mscl->save();
            $msi->location=Input::get("location");
            $msi->save();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("id"=>$msi->id));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "changeLocaStorage", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      zy
     * @DateTime:  2017-09-12 14:02:20
     * @Description: 批量更换入库批次货位 暂时只修改该纪录的信息
     */
    public function  changeLocaStorageMore(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'item_id' => 'required',
                // 'item_type' => 'required',
                'location'=>"required",
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            
            $msi=MaintenanceStorageItem::where("shop_id",$sc_user->shop_id)->where("status",1)->where("type",1)->
                where("item_id",Input::get("item_id"))->orderBy('created_at','desc')->
                select("id","list_id")->get();

            $date = date("Y-m-d H:i:s");
            $ids = "";
            foreach ($msi as $v) {
                $msl=MaintenanceStorageList::find($v->list_id);
                if(is_null($msl)||$msl->shop_id!=$sc_user->shop_id){
                    $response = Lang::get("scs_error.maintenance_storage_notexit_60000");
                    break;
                }
                if(MaintenanceInventoryList::isInventory($sc_user->shop_id)){
                    $response = Lang::get("scs_error.data_inventory_isrun_61000");
                    break;
                }
                $change_location[] = array('msi_id' => $v->id, 'old_location' => $v->location,"new_location"=>Input::get("location"),"created_at" => $date, "updated_at"=>$date,"created_by"=>$sc_user->id);
                $ids[]= $v->id;
            }

            $insert = DB::table('maintenance_storage_change_location')->insert($change_location);
            $update_array = array('location' => Input::get("location"),"updated_at"=>$date);

            $update = DB::table('maintenance_storage_item')->whereIn('id',$ids)->update($update_array);
            if ($insert && $update) {
                $response = array('code' => 0, 'msg' => 'ok', 'result' => array("item_id"=>Input::get("item_id")));
            } else {
                $response = Lang::get("scs_error.data_inventory_isrun_61022");
            }
            
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "changeLocaStorageMore", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    
    /**
     * @Author:      lyt
     * @DateTime:    2016-02-27 15:51:14
     * @Description: 执行 批量临时入库单入库
     */
    public function  implementStorage(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id'=>"required",
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $msl=MaintenanceStorageList::find(Input::get("id"));
            if(is_null($msl)||$msl->shop_id!=$sc_user->shop_id||$msl->type!=1){
                $response = Lang::get("scs_error.maintenance_storage_notexit_60000");
                break;
            }
            if($msl->status!=0){
                $response = Lang::get("scs_error.maintenance_storage_statuserror_60001");
                break;
            }
            if(MaintenanceInventoryList::isInventory($sc_user->shop_id)){
                $response = Lang::get("scs_error.data_inventory_isrun_61000");
                break;
            }
            $response=MaintenanceStorageList::implement($msl,$sc_user->id);
            // $response = array('code' => 0, 'msg' => 'ok', 'result' => array("id"=>$msl->id));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "deleteStorage", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-8-9 17:38:09
     * @Description: 执行 批量临时入库单入库前判断
     */
    public function  implementStorageBefore(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id'=>"required",
            )
        );
        $response = array('code' => 0, 'msg' => 'ok','result'=>array());
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $msl=MaintenanceStorageList::find(Input::get("id"));
            if(is_null($msl)||$msl->shop_id!=$sc_user->shop_id||$msl->type!=1){
                $response = Lang::get("scs_error.maintenance_storage_notexit_60000");
                break;
            }
            if($msl->status!=0){
                $response = Lang::get("scs_error.maintenance_storage_statuserror_60001");
                break;
            }
            $msi=MaintenanceStorageItem::where("list_id",$msl->id)->where("status",0)->get();
            if(sizeof($msi)<=0){//说明该入库单是空的
                $response = Lang::get("scs_error.maintenance_storage_empty_60016");
                return $response;
            }
            // foreach ($msi as $_msi) {
            //     if($_msi->item_type==1){
            //         $sins=SupplyItemNonStandard::find($_msi->item_id);
            //         if($sins->use_status==0){//说明耗材使用状态为关闭
            //             $response["result"][]=$sins->code.'、' .$sins->brand_name."、".$sins->name;
            //         }
            //     }
            // }
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "deleteStorage", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
   /**
    * @Author:      lyt
    * @DateTime:    2016-03-10 09:25:09
    * @Description: 用于领用退货 入库单的执行
    */
    public function  implementStorageReturn(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id'=>"required",
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $msl=MaintenanceStorageList::find(Input::get("id"));
            if(is_null($msl)||$msl->shop_id!=$sc_user->shop_id||$msl->type!=2){
                $response = Lang::get("scs_error.maintenance_storage_notexit_60000");
                break;
            }
            if($msl->status!=0){
                $response = Lang::get("scs_error.maintenance_storage_statuserror_60001");
                break;
            }
            if(MaintenanceInventoryList::isInventory($sc_user->shop_id)){
                $response = Lang::get("scs_error.data_inventory_isrun_61000");
                break;
            }
            $response=MaintenanceStorageList::implementReturn($msl,$sc_user->id);
            // $response = array('code' => 0, 'msg' => 'ok', 'result' => array("id"=>$msl->id));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "deleteStorage", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      zwh
     * @DateTime:    2016-6-13 11:17:22
     * @Description: 用于判断当前入库单是否已添加某耗材
     */
    public function addStorageItemBefore(){
    
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'list_id'=>"required",
                'item_id'=>"required",
                // 'item_type'=>"required",
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $msl=MaintenanceStorageList::find(Input::get("list_id"));
            if(is_null($msl)||$msl->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.maintenance_storage_notexit_60000");
                break;
            }
            if($msl->status!=0){
                $response = Lang::get("scs_error.maintenance_storage_statuserror_60001");
                break;
            }
//             if(MaintenanceInventoryList::isInventory($sc_user->shop_id)){
//                 $response = Lang::get("scs_error.data_inventory_isrun_61000");
//                 break;
//             }
           
            $msi=MaintenanceStorageItem::where('list_id',Input::get('list_id'))->where('type',1)->where('item_id',Input::get('item_id'))->whereIn('status',array(0,1))->first();
            if($msi){
                $response = Lang::get("scs_error.maintenance_storage_statuserror_60017");
                break;
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("list_id"=>Input::get('list_id')));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "addStorageItemBefore", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

   /**
    * @Author:      lyt
    * @DateTime:    2016-02-22 09:36:33
    * @Description: 用于添加批量临时的 入库de
    *  如果是标准耗材
    * 商品编码就是 耗材型号
    *商品名称就是 品牌 系列 型号
    *规格型号就是 型号
    */
    public function addStorageItem(){

        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'list_id'=>"required",
                'item_id'=>"required",
                // 'type'=>"required",
                "data_id"=>"required",
                "number"=>"required|numeric|min:0|not_in:0",
                "location"=>"required",
                'production_date'=>"date",
                // 'no_tax_purchase_price'=>"required",
                'tax_purchase_price'=>"required",
                // 'tax_purchase_subtotal'=>"required",
                'no_tax_plan_costs'=>"required",
                'tax_plan_costs'=>"required",
                // 'pack_unit'=>"required",
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $msl=MaintenanceStorageList::find(Input::get("list_id"));
            if(is_null($msl)||$msl->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.maintenance_storage_notexit_60000");
                break;
            }
            if($msl->status!=0){
                $response = Lang::get("scs_error.maintenance_storage_statuserror_60001");
                break;
            }
            if(MaintenanceInventoryList::isInventory($sc_user->shop_id)){
                $response = Lang::get("scs_error.data_inventory_isrun_61000");
                break;
            }
            $ntpp=Input::get("no_tax_purchase_price")*100;
            $tpp=Input::get("tax_purchase_price")*100;
            if($ntpp>$tpp){
                $response = Lang::get("scs_error.data_inventory_error_61017");
                break;
            }
            $si=SupplyItem::find(Input::get("item_id"));
            if(is_null($si)||$si->status!=1){
                $response = Lang::get("scs_error.supply_item_not_exit_41000");
                break;
            }

            $msi=new MaintenanceStorageItem;
            $msi->list_id=Input::get("list_id");
            $msi->tax_purchase_price=Input::get("tax_purchase_price")*100;
            $msi->no_tax_purchase_price=Input::get("no_tax_purchase_price")*100;
            if(Input::get("production_date")){
                $msi->production_date=Input::get("production_date");
            }
            $msi->tax_plan_costs=Input::get("tax_plan_costs")*100;
            $msi->no_tax_plan_costs=Input::get("no_tax_plan_costs")*100;
            $msi->number=Input::get("number");
            $msi->tax_purchase_subtotal=$msi->tax_purchase_price*$msi->number;
            $msi->current_number=Input::get("number");
            $msi->location=Input::get("location");
            // $msi->production_date=Input::get("production_date");
            $msi->own_id=$sc_user->id;
            $msi->data_id=Input::get("data_id");
            $msi->item_type=$si->type;
            $msi->item_id=Input::get("item_id");
            $msi->pack_unit=Input::get("pack_unit");
            $msi->shop_id=$sc_user->shop_id;
            $msi->type=1;
            $msi->status=0;
            $msi->created_by=$sc_user->id;
            // if(Input::get("type")==0){//说明是 标准耗材
                
                $item_info=SupplyItem::getCacheItemInfo($si->id);
                $sl=SupplyLocation::where('item_id',$si->id)->where('shop_id',$sc_user->shop_id)->first();
                if($sl){
                    $sl->item_type=$si->type;
                    $sl->location=Input::get("location");
                    $sl->save();
                }else{
                    $sl=new SupplyLocation;
                    $sl->item_id=$si->id;
                    $sl->item_type=$si->type;
                    $sl->shop_id=$sc_user->shop_id;
                    $sl->location=Input::get("location");
                    $sl->save();
                }
                $msi->code=$item_info->code;
                $msi->name=$item_info->name;
                $msi->subject=$item_info->st_classifi_id;
                $msi->model=$item_info->model;
            // }else{
            //     $sins=SupplyItemNonStandard::find(Input::get("item_id"));
            //     if(is_null($sins)||$sins->status!=1){
            //         $response = Lang::get("scs_error.supply_item_not_exit_41000");
            //         break;
            //     }
            //     if($sins->use_status==0){
            //         $response = Lang::get("scs_error.supply_item_not_exit_41007");
            //         break;
            //     }
            //     $sl=SupplyLocation::where('item_id',$sins->id)->where('shop_id',$sc_user->shop_id)->where('item_type',0)->first();
            //     if($sl){
            //         $sl->location=Input::get("location");
            //         $sl->save();
            //     }else{
            //         $sl=new SupplyLocation;
            //         $sl->item_id=$sins->id;
            //         $sl->item_type=0;
            //         $sl->shop_id=$sc_user->shop_id;
            //         $sl->location=Input::get("location");
            //         $sl->save();
            //     }
            //     $msi->code=$sins->code;
            //     $msi->name=$sins->name;
            //     $msi->subject=$sins->subject;
            //     $msi->model=$sins->model;
            // }
            $msi->save();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("id"=>$msi->id));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "addStorageItem", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /**
     * @Author:      zwh
     * @DateTime:    2016-8-9 15:24:46
     * @Description: 用于删除 批量临时入库的耗材 和 订单退货的耗材前判断
     */
    public function deleteStorageItemBefore(){
    
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                "id"=>'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $msi=MaintenanceStorageItem::find(Input::get("id"));
            if(is_null($msi)||$msi->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.maintenance_storage_notexit_60000");
                break;
            }
            // if($msi->item_type==1){
            //     $sins=SupplyItemNonStandard::find($msi->item_id);
            //     if($sins->use_status==0){
            //         $response = Lang::get("scs_error.supply_item_use_status_error_41008");
            //         break;
            //     }
            // }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("id"=>$msi->id));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "addStorageItem", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /**
     * @Author:      lyt
     * @DateTime:    2016-02-27 15:09:53
     * @Description: 用于删除 批量临时入库的耗材 和 订单退货的耗材
     * 没有执行的才可以入库
     */
    public function deleteStorageItem(){

        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                "id"=>'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $msi=MaintenanceStorageItem::find(Input::get("id"));
            if(is_null($msi)||$msi->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.maintenance_storage_notexit_60000");
                break;
            }
            if($msi->status!=0){
                $response = Lang::get("scs_error.maintenance_storage_statuserror_60001");
                break;
            }
            if(MaintenanceInventoryList::isInventory($sc_user->shop_id)){
                $response = Lang::get("scs_error.data_inventory_isrun_61000");
                break;
            }
            $msi->status=2;
            $msi->save();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("id"=>$msi->id));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "addStorageItem", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      lyt
     * @DateTime:    2016-03-09 14:18:15
     * @Description: 用于 添加领用退库的 耗材批次
     */
    public function addStorageReturnItem(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'list_id'=>"required",
                'item_id'=>"required",
                "number"=>"required|numeric|min:0|not_in:0",
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $msl=MaintenanceStorageList::find(Input::get("list_id"));
            if(is_null($msl)||$msl->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.maintenance_storage_notexit_60000");
                break;
            }
            if($msl->status!=0){
                $response = Lang::get("scs_error.maintenance_storage_statuserror_60001");
                break;
            }
            $moi=MaintenanceOutItem::find(Input::get("item_id"));
            if(is_null($moi)||$moi->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.maintenance_outitem_notexit_60008");
                break;
            }
            $mol=MaintenanceOutList::find($moi->list_id);
            if($moi->status!=1||$mol->type!=1){//如果 要退的 出库批次 不存在 或者不是 领用出库的 批次
                $response = Lang::get("scs_error.maintenance_outitem_statuserror_60007");
                break;
            }
            if($moi->number-$moi->return_numer<Input::get("number")){
                $response = Lang::get("scs_error.maintenance_out_numbererror_60006");
                break;
            }
            if(MaintenanceInventoryList::isInventory($sc_user->shop_id)){
                $response = Lang::get("scs_error.data_inventory_isrun_61000");
                break;
            }
            $msi=new MaintenanceStorageItem;
            $msi->list_id=Input::get("list_id");
            $msi->tax_purchase_price=$moi->tax_purchase_price;
            $msi->no_tax_purchase_price=$moi->no_tax_purchase_price;
            $msi->production_date=0;
            $msi->tax_plan_costs=0;
            $msi->no_tax_plan_costs=0;
            $msi->number=Input::get("number");
            $msi->tax_purchase_subtotal=$msi->tax_purchase_price*$msi->number;
            $msi->current_number=0;
            $msi->location="";
            $msi->production_date=date("Y-m-d",time());//此处的  date 无用
            $msi->own_id=$sc_user->id;
            $msi->data_id=$moi->date_id;
            $msi->item_type=$moi->item_type;
            $msi->item_id=$moi->item_id;
            $msi->pack_unit=$moi->pack_unit;
            $msi->shop_id=$sc_user->shop_id;
            $msi->type=2;
            $msi->status=0;
            $msi->created_by=$sc_user->id;
            $msi->code=$moi->code;
            $msi->name=$moi->name;
            $msi->subject=$moi->subject;
            $msi->model=$moi->model;
            $msi->out_id=$moi->id;
            $msi->save();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("id"=>$msi->id));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "addStorageReturnItem", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      lyt
     * @DateTime:    2016-03-05 10:10:43
     * @Description: 用于执行 退库申请的接口
     */
    public function  implementOrderReturn(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'ids'=>"required", //退库单的id 集合
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            if(MaintenanceInventoryList::isInventory($sc_user->shop_id)){
                $response = Lang::get("scs_error.data_inventory_isrun_61000");
                break;
            }
            $ids = json_decode(Input::get('ids'),1);
            $return_order_arr=array();
            foreach ($ids as $_id) {
                $mro=MaintenanceReturnOrder::find($_id);
                if(is_null($mro)||$mro->shop_id!=$sc_user->shop_id){
                    $response = Lang::get("scs_error.maintenance_return_order_notexit_60012");
                    break 2;
                }
                if($mro->status!=0){
                    $response = Lang::get("scs_error.maintenance_return_order_statuserror_60013");
                    break 2;
                }
                $return_order_arr[]=$mro;
            }
            $response=MaintenanceStorageList::implementOrder($return_order_arr,$sc_user);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "implementOrderReturn", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      lyt
     * @DateTime:    2016-03-07 14:53:27
     * @Description: 用于拒绝 订单的退库请求
     */
    public function refuseOrderReturn(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'ids'=>"required", //退库单的id 集合
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
//             if(MaintenanceInventoryList::isInventory($sc_user->shop_id)){
//                 $response = Lang::get("scs_error.data_inventory_isrun_61000");
//                 break;
//             }
            $ids = json_decode(Input::get('ids'),1);
            $return_order_arr=array();
            $id_arr=array();
            foreach ($ids as $_id) {
                $mro=MaintenanceReturnOrder::find($_id);
                if(is_null($mro)||$mro->shop_id!=$sc_user->shop_id){
                    $response = Lang::get("scs_error.maintenance_return_order_notexit_60012");
                    break 2;
                }
                if($mro->status!=0){
                    $response = Lang::get("scs_error.maintenance_return_order_statuserror_60013");
                    break 2;
                }
                $return_order_arr[]=$mro;
                $id_arr[]=array("id"=>$_id);
            }
            MaintenanceReturnOrder::refuseOrder($return_order_arr);
            $response=array('code' => 0, 'msg' => 'ok', 'result' => array("id_info"=>$id_arr));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "refuseOrderReturn", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);

    }
}